package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.PlanModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 抄表计划业务
 * @author zhaokf
 * @date 2013-04-22
 */
public class PlanService extends BaseService
{
	/**
	 * 分页列出抄表计划
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<PlanModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("plan_id") != null){
				where = " AND plan_id=" + search.get("plan_id");
			}
			if (search.get("plan_jfq") != null){
				where = " AND plan_jfq LIKE '%" + search.get("plan_jfq") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<PlanModel> plan = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM R_PLAN " + where;

		try{
			plan = PlanModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return plan;
	}

	/**
	 * 根据ID查询抄表计划
	 * @param params
	 * @return
	 */
	public static PlanModel get(Map<String,String> params){
		String no = params.get("plan_id")==null?"":params.get("plan_id").toString();
		PlanModel pm = PlanModel.dao.findFirst("SELECT * FROM R_PLAN WHERE PLAN_ID=?", no);
		return pm;
	}

	/**
	 * 增加抄表计划信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){

		Record record = new Record();
		record.set("plan_id", Db.queryBigDecimal("select r_plan_seq.nextval from dual").intValue());
		record.set("plan_jfq", params.get("plan_jfq").toString());
		record.set("plan_org_no", params.get("plan_org_no").toString());
		record.set("plan_sect_id", params.get("plan_sect_id").toString());
		boolean isupadded = Db.save("r_plan", "plan_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的抄表计划信息已存在！");
	}

	/**
	 * 修改抄表计划信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		Record record = new Record();
		record.set("plan_id",params.get("plan_id").toString());
		record.set("plan_jfq", params.get("plan_jfq").toString());
		record.set("plan_org_no", params.get("plan_org_no").toString());
		record.set("plan_sect_id", params.get("plan_sect_id").toString());
		boolean isupadded = Db.save("r_plan", "plan_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的抄表计划信息不存在！");
	}

	/**
	 * 删除抄表计划信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		
		String no = params.get("plan_id") == null ? "" : params.get("plan_id").toString();
		String sql = "select * from r_plan where plan_id=?";
		PlanModel pm = PlanModel.dao.findFirst(sql, no);
		if (pm != null)
		{
			pm.delete();
			return true;
		}
		return setError("您访问的抄表计划信息不存在！");
	}
}

